/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tanks.Missiles;

import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import tanks.BlockType;
import tanks.Cell;
import tanks.Direction;
import tanks.Game;
import tanks.Graph;
/**
 *
 * @author Александр
 */
public class Missile
    {
        int facing;
        protected int range=2;
        protected int strength=10;
        protected Cell c;
        protected Game game = Game.Instance();

        public Missile(int Facing, Cell C)
        {
            facing = Facing;
            c = C;
        }
        protected Image drawShape(Image canvas)
        {
            Graphics gr = canvas.getGraphics();
            gr.setColor(0xFF8C00);//Orange
            gr.fillArc(c.X() * Graph.CellSize() + Graph.CellSize() / 5, c.Y() * Graph.CellSize() + Graph.CellSize() / 5, Graph.CellSize() / 5, Graph.CellSize() / 5, 0, 360);
            //gr.fillEllipse(new SolidBrush(Color.Orange), new Rectangle(c.X * game.CellSize + game.CellSize / 5, c.Y * game.CellSize + game.CellSize / 5, game.CellSize / 5, game.CellSize / 5));
            return canvas; 
        }
        public void Draw(Image canvas)
        {
            Cell nextC=c;
            switch (facing){
                case Direction.North:
                    if (c.Y() - 1 >= 0)
                    {
                        nextC = game.Grid[c.X()][c.Y() - 1];
                    }
                    break;
                case Direction.South:
                    if (c.Y() + 1 < game.fieldHeight)
                    {
                        nextC = game.Grid[c.X()][c.Y() + 1];
                    }
                    break;
                case Direction.West:
                    if (c.X() - 1 >= 0)
                    {
                        nextC = game.Grid[c.X() - 1][c.Y()];
                    }
                    break;
                case Direction.East:
                    if (c.X() + 1 < game.fieldWidth)
                    {
                        nextC = game.Grid[c.X() + 1][c.Y()];
                    }
                    break;
            }
            if(nextC.currTank==null&&nextC.Block==BlockType.Empty&&c != nextC){
                c = nextC;
            }
            else
            {
                if (nextC.currTank == null)
                {
                    game.Explode(range, c, strength);
                }
                else
                {
                    game.Explode(range, nextC, strength);
                }
                game.Miss.removeElement(this);
            }
            drawShape(canvas);
        }
    }
